#include "shunxubiao.h"
#include <stdio.h>
#include <stdarg.h>

extern shunxubiao* creat_list(unsigned capicity)
{
    shunxubiao* list = (shunxubiao*)malloc(sizeof(shunxubiao) + sizeof(int) * capicity);
    if(list == NULL){

        return NULL;
    }
    list->capicity = capicity;
    list->length = 0;
    return list;
}

extern void free_list(shunxubiao* list)
{
    assert(list != NULL);
    free(list->data);
    free(list);
}

extern void visit_list(shunxubiao* list,void (*callback)(int))
{
    assert(list != NULL);
    for(int i = 0;i < list->length;i++)
    {
        callback(list->data[i]);
    }
}

extern shunxubiao* insert(shunxubiao* list,int* p,int val)
{
    assert(list != NULL);

    if(p == list->data[list->length -1])
    {
        list->data[list->length] = val;
        list->length++;
        return list;
    }

    for(int* least = list->data +list->length;least < p;least--)
    {
        *least = *(least - 1);
    }

    *p = val;
    list->length++;
    return list;

}